<?php
$host = "192.168.1.100";
$host = "localhost";
$port = 8000;

$thread = 10;

echo "\n";

for($t = 0; $t < $thread; $t++)
{
	if (pcntl_fork() == 0)
	{
		$start = microtime(true);
		test1();
		echo "thread:$t time:" . (microtime(true) - $start) . "\n";
		exit;
	}

}

function test1()
{
	global $host, $port;
	$len = 0;

	$fp = fsockopen($host, $port, $errno, $errstr, 1);
	if (!$fp) {
		echo "$errstr ($errno)<br />\n";
	} else {
		$msg = str_repeat(md5(mt_rand()), 10000) . "\n";
		$len = strlen($msg);

		$start_time = microtime(true);

		for($i = 0; $i < 10000; $i++)
		{
			writeandread($fp, $msg, $len);
		}


		fclose($fp);
	}
}

function writeandread($fp, $msg, $len)
{
	if( fwrite($fp, $msg, $len) === false){
		exit;
	}
	$msg_md5 = md5($msg);

	$recv_len = 0;
	$recv_str = '';
	while (($str = fgets($fp, 1024)) != false) {
		$recv_str .= $str;
		$recv_len += strlen($str);
		if ($recv_len == $len)
			break;
	}
	if (md5($recv_str) != $msg_md5){
		echo "error\n";
	}
}
